package com.infonam.media.activities;

import com.infonam.media.R;
import com.infonam.media.data.VideoInfo;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Bundle;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.VideoView;

public class VideoActivity extends Activity {

	private VideoView videoview;
	private TextView title;
	private TextView descriptions;
	private static ProgressDialog progressDialog;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.view_video);

		Intent intent = getIntent();
		String path = intent.getStringExtra(VideoInfo.PATH_VIDEO);
		String info = intent.getStringExtra(VideoInfo.INFO_VIDEO);
		String namevideo = intent.getStringExtra(VideoInfo.TITLE_VIDEO);

		videoview = (VideoView) findViewById(R.id.view_video);
		title = (TextView) findViewById(R.id.title_video);
		title.setText(namevideo);

		descriptions = (TextView) findViewById(R.id.description_video);
		descriptions.setText(info);

		progressDialog = ProgressDialog.show(VideoActivity.this, "",
				"Buffering video...", true);
		progressDialog.setCancelable(true);

		PlayVideo(path);

	}

	private void PlayVideo(String videoPath) {
		try {
			getWindow().setFormat(PixelFormat.TRANSLUCENT);
			MediaController mediaController = new MediaController(
					VideoActivity.this);
			mediaController.setAnchorView(videoview);

			Uri video = Uri.parse(videoPath);
			videoview.setMediaController(mediaController);
			videoview.setVideoURI(video);
			videoview.requestFocus();
			mediaController.setMediaPlayer(videoview);
			videoview.setOnPreparedListener(new OnPreparedListener() {
				@Override
				public void onPrepared(MediaPlayer mp) {
					progressDialog.dismiss();
					videoview.start();
				}

			});

		} catch (Exception e) {
			progressDialog.dismiss();
			System.out.println("Video Play Error :" + e.toString());
			finish();
		}

	}

	@Override
	public void onDestroy() {
		super.onDestroy(); // Always call the superclass

	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	@Override
	protected void onRestart() {
		super.onRestart();
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		// TODO Auto-generated method stub
		super.onConfigurationChanged(newConfig);

		if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
			// your code
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoview
					.getLayoutParams();
			params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
			params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 0);
			params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
			params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);

		} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// your code
			RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) videoview
					.getLayoutParams();

			params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
			params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

		}

	}

}
